<template>
	<view class="container-page">
		<!-- 填充盒子 -->
		<view style="height: 24rpx;"></view>
		<view class="container-top">
			<swiper class="swiper" circular :indicator-dots="false" :autoplay="true" :interval="5000"
				style="height: 85vh;border-radius: 20rpx;overflow: hidden;" @change="posterListChange">
				<swiper-item v-for="(item,index) in posterList" :key="index">
					<uv-image :src="item.image" :lazy-load="true" width="100%" height="65vh"
						mode="aspectFill"></uv-image>
					<view class="logo-code">
						<view class="logo-box">
							<image src="../../static/images/logo.png" class="logo"></image>
							<view class="platform-list">
								<image src="../../static/images/share/apple.png" class="platform"></image>
								<image src="../../static/images/share/android.png" class="platform"></image>
							</view>
						</view>
						<view class="code-box">
							<image :src="qrCodeImage" class="qr-code"></image>
							<view class="title">长按识别二维码</view>
						</view>
					</view>
				</swiper-item>

			</swiper>
			<!-- 	<uv-swiper :list="posterList" keyName="image" :autoplay="true" interval='5000' circular height="65vh"
				:current="current" @change="posterListChange"></uv-swiper>
			<view class="logo-code">
				<view class="logo-box">
					<image src="../../static/images/logo.png" class="logo"></image>
					<view class="platform-list">
						<image src="../../static/images/share/apple.png" class="platform"></image>
						<image src="../../static/images/share/android.png" class="platform"></image>
					</view>
				</view>
				<view class="code-box">
					<image :src="qrCodeImage" class="qr-code"></image>
					<view class="title">长按识别二维码</view>
				</view>
			</view> -->
		</view>
		<view class="container-bottom">
			<view class="handle-item" @click="generatePoster">
				<image src="../../static/images/share/download.png" class="image"></image>
				<view class="title">保存到相册</view>
			</view>
			<view class="handle-item" @click="copyShareLink">
				<image src="../../static/images/share/sharelink.png" class="image"></image>
				<view class="title">分享链接</view>
			</view>
		</view>
		<!-- 生成的图片 -->
		<uni-popup type="center" ref="posterImg" :maskClick="false">
			<view class="share-box">
				<image :src="shareImage" class="share-image" @click="previewImg"></image>
				<canvas canvas-id="posterCanvas" class="share-info"></canvas>
				<!-- #ifdef H5 -->
				<view class="title">
					长按图片保存到手机
				</view>
				<!-- #endif -->
				<!-- #ifndef H5 -->
				<view class="title">
					点击图片保存到手机
				</view>
				<!-- #endif -->
				<view class="close" @click="closePopup">
					<uv-icon name="close"></uv-icon>
				</view>
			</view>
		</uni-popup>
	</view>
	</view>
</template>

<script>
	import api from '@/api/index.js';
	export default {
		data() {
			return {
				current: 0,
				posterList: [], //海报列表
				posterImage: '', // 海报图片路径
				qrCodeImage: '', // 二维码图片路径
				posterLink: '', // 海报链接
				shareImage: '',
				showShare: false,
			};
		},
		mounted() {
			this.getShareConfig()
		},
		methods: {
			previewImg() {
				// #ifdef H5
				return;
				// #endif
				uni.previewImage({
					current: this.shareImage,
					urls: [this.shareImage]
				});
			},
			closePopup() {
				this.$refs.posterImg.close();
				this.shareImage = false;
			},
			posterListChange(e) {
				this.current = e.detail.current
			},
			async generatePoster() {
				let that = this
				// 获取屏幕信息
				const systemInfo = uni.getSystemInfoSync();
				// 获取屏幕高度（单位：px）
				const screenHeight = systemInfo.screenHeight;
				const ctx = uni.createCanvasContext('posterCanvas');
				this.$refs.posterImg.open();
				try {
					// 获取背景图信息
					// const backgroundRes = await this.getImageInfo('../../static/images/posterImage.jpg');
					const backgroundRes = await this.getImageInfo(this.posterList[this.current].image);
					ctx.drawImage(backgroundRes.path, 0, 0, 349, 439.74);
					// 获取小程序码图片信息
					// const qrCodeRes = await this.getImageInfo('../../static/images/qrCodeImage.png');
					const qrCodeRes = await this.getImageInfo(this.qrCodeImage);
					ctx.drawImage(qrCodeRes.path, 256, 459.74, 72, 72);
					let title = '长按识别二维码'
					ctx.fillText(title, 257, 545)
					// 获取Logo图片信息
					const logoRes = await this.getImageInfo('../../static/images/logo.png');
					ctx.drawImage(logoRes.path, 24, 468, 52, 52);
					// 获取android图片信息
					const androidRes = await this.getImageInfo('../../static/images/share/android.png');
					ctx.drawImage(androidRes.path, 28, 527, 14, 14);
					// 获取apple图片信息
					const appleRes = await this.getImageInfo('../../static/images/share/apple.png');
					ctx.drawImage(appleRes.path, 58, 527, 14, 14);
					// 绘制完成
					ctx.draw(false, () => {
						// 保存海报到临时文件路径
						uni.canvasToTempFilePath({
							canvasId: 'posterCanvas',
							success: (result) => {
								that.shareImage = result.tempFilePath;
								that.showShare = true;
								console.log('海报生成成功，路径：', result.tempFilePath);
							},
							fail: (err) => {
								console.error('海报生成失败：', err);
							},
						});
					});
				} catch (err) {
					console.error('绘制海报过程中出错：', err);
				}
			},

			// 工具函数：获取图片信息
			getImageInfo(src) {
				return new Promise((resolve, reject) => {
					uni.getImageInfo({
						src: src,
						success: (res) => resolve(res),
						fail: (err) => reject(err),
					});
				});
			},
			copyShareLink() {
				uni.setClipboardData({
					data: this.posterLink,
					success: function() {
						uni.showToast({
							icon: 'none',
							title: "复制成功"
						})
					}
				});
			},
			getShareConfig() {
				api.getShareConfig().then(res => {
					this.posterList = res.data.image
					this.posterLink = res.data.url
					this.qrCodeImage = res.data.qrcode
				})
			},
		}
	};
</script>

<style lang="scss" scoped>
	.share-bg {
		position: fixed;
		left: 0;
		top: 0;
		z-index: 99;
		width: 100%;
		height: 100%;
		background: rgba(0, 0, 0, 0.5);
	}

	.share-box {

		.share-image {
			position: relative;
			top: 0;
			z-index: 100;
			width: 349px;
			height: 568px;
			background-color: #ffffff;
			border-radius: 10rpx;
			overflow: hidden;
		}

		.share-info {
			position: absolute;
			top: 0;
			width: 349px;
			height: 568px;
			background-color: #ffffff;
			border-radius: 10rpx;
			overflow: hidden;
		}

		.title {
			font-size: 32rpx;
			font-weight: bold;
			color: #FFFFFF;
			line-height: 32rpx;
			text-align: center;
			margin-top: 28rpx;
		}

		.close {
			width: 80rpx;
			height: 80rpx;
			margin: 40rpx auto 0;
			display: flex;
			background-color: #ffffff;
			border-radius: 50%;
			align-items: center;
			justify-content: center;
		}
	}

	.container-page {
		// display: flex;
		// flex-direction: column;
		// justify-content: center;
		// min-height: 100vh;

		.container-top {
			margin: 0rpx 26rpx;
			border-radius: 20rpx;

			.logo-code {
				background-color: #ffffff;
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 40rpx 42rpx;

				.platform-list {
					margin-top: 10rpx;
					display: flex;
					align-items: center;
					justify-content: space-between;

					.platform {
						width: 28rpx;
						height: 28rpx;
					}
				}

				.logo {
					width: 104rpx;
					height: 104rpx;
				}

				.code-box {
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;

					.qr-code {
						width: 114rpx;
						height: 114rpx;
					}

					.title {
						font-size: 20rpx;
						color: #323334;
						margin-top: 6rpx;
					}
				}
			}
		}

		.container-bottom {
			position: fixed;
			bottom: 0;
			box-sizing: border-box;
			padding: 20rpx 52rpx;
			width: 100%;
			height: 198rpx;
			background: #FFFFFF;
			border-radius: 28rpx 28rpx 0rpx 0rpx;
			display: flex;
			align-items: center;
			justify-content: space-around;

			.handle-item {
				width: 25%;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;

				.image {
					width: 104rpx;
					height: 104rpx;
				}

				.title {
					font-size: 24rpx;
					color: #323334;
					margin-top: 20rpx;
				}
			}
		}
	}
</style>